package com.ymatou.iapi.userservice.testcase;

import java.io.IOException;
import java.text.Normalizer.Form;
import java.util.List;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.GetOneBean;
import com.ymatou.iapi.userservice.parameter.resp.AddressResp;
import com.ymatou.iapi.userservice.parameter.resp.GetOneResult;
import com.ymatou.iapi.userservice.service.GetOneCall;
import com.ymatou.iapi.userservice.service.GetOneCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.UserAddress;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

@TestSuite
public class Ts_GetOne {

	private static GetOneBean arb;
	private static GetOneCall arc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取收货地址");
	}

	@Before
	public void caseUp() {
		arb = new GetOneBean();
		arc = new GetOneCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(arc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOne_001() {
		Logger.start(true, "查询地址成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();

			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(userid);
			String aid=JUserCallService.AddressRequestDtoCall(addressBean);
			
			arb.setUserId(userid);
			arb.setAddressId(Integer.valueOf(aid));
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals("true", arc.getString("Success"), "查询地址成功");
			Logger.verifyEquals("200", arc.getString("ResponseCode"), "验证ResponseCode");
			GetOneResult rearb = arc.getResult();
			Logger.verifyEquals(rearb.getsTelphone(), addressBean.getsTelphone(),
					"验证电话号码");
			//			Logger.verifyEquals(arb.getsAddressCode(), addressInfo.get("sAddressCode"), "验证Address code");
			Logger.verifyEquals(rearb.getsBaseAddress(),
					addressBean.getsBaseAddress(), "验证base address");
			Logger.verifyEquals(rearb.getsDetailAddress(),
					addressBean.getsDetailAddress(), "验证detail address");
			Logger.verifyEquals(rearb.getsMobilePhone(), addressBean.getsMobilePhone(),
					"验证Mobile信息");
			Logger.verifyEquals(rearb.getsPostCode(), addressBean.getsPostCode(),
					"验证邮编");
			//			Logger.verifyEquals(arb.getsEmail(), addressInfo.getEmail(), "验证邮箱地址");
			Logger.verifyEquals(rearb.getsRecipient(),
					addressBean.getsRecipient(), "验证recipient");
			Logger.verifyEquals(rearb.getbIsDefault(),
					addressBean.isbIsDefault(), "验证地址是默认地址");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOne_002() {
		Logger.start(true, "不输入uid,失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();

			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(userid);
			String aid=JUserCallService.AddressRequestDtoCall(addressBean);
			
//			arb.setUserId(userid);
			arb.setAddressId(Integer.valueOf(aid));
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals("false", arc.getString("Success"), "查询地址失败");
			Logger.verifyIsContains("无效的用户编号", arc.getString("LastErrorMessage"), "LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOne_003() {
		Logger.start(true, "不输入aid,失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();

			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(userid);
			String aid=JUserCallService.AddressRequestDtoCall(addressBean);
			
			arb.setUserId(userid);
//			arb.setAddressId(Integer.valueOf(aid));
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals("false", arc.getString("Success"), "查询地址失败");
			Logger.verifyIsContains("无效的地址编号", arc.getString("LastErrorMessage"), "LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOne_004() {
		Logger.start(true, "uid和aid不匹配,失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int userid = urrc.getUserid();

			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(userid);
			String aid=JUserCallService.AddressRequestDtoCall(addressBean);
			
			arb.setUserId(userid+1);
			arb.setAddressId(Integer.valueOf(aid));
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals("true", arc.getString("Success"), "验证Success");
			Logger.verifyIsNull(arc.getResult(),"验证没有返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOne_005() {
		Logger.start(true, "test");
		try {
			arb.setUserId(30210219);
			arb.setAddressId(1180292);
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals("true", arc.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}